package com.ww.basic.fun

object mutiParamsFunTest7 {
  def inject(arr: Array[Int], initial: Int, operation: (Int, Int) => Int) = {
    var carrayOver = initial
    arr.foreach(element => carrayOver = operation(carrayOver, element))
    carrayOver
  }

  def main(args: Array[String]): Unit = {
    val array = Array(2, 3, 5, 1, 6, 4)
    val sum = inject(array, 0, (array, elem) => array + elem)
    println(s"Sum of elements in array is $sum")

    // 内置方法foldLeft()
    val sum2 = array.foldLeft(0) { (sum2, elem) => sum + elem }
    val max = array.foldLeft(Integer.MIN_VALUE) { (large, elem) => Math.max(large, elem) }
    println(s"求和${sum2}")
    println(s"最大值${max}")
  }
}
